package com.asset.salesforce.dao;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Contact;

@Repository
public class ContactDaoImpl extends BasicDaoImpl<Contact>{
	
	@Autowired
	private SessionFactory sessionFactory;
	

	@SuppressWarnings("unchecked")
	public List<Contact> getAllContactByEmpId(Integer id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact where creator.id = :id and isDeleted= :isDeleted") ;
		query.setParameter("id", id);
		query.setParameter("isDeleted", false);
		List<Contact> contacts = new ArrayList<Contact>();
		contacts = query.list();
		return contacts;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Contact> getByParent( String typeId, Integer parentId) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact where isDeleted= :isDeleted and viewType=:typeId and parentId= :parentId");
		query.setParameter("isDeleted", false);
		query.setParameter("typeId", typeId);
		query.setParameter("parentId", parentId);
		List<Contact> contacts= new ArrayList<Contact>();
		contacts = query.list();
		System.out.println(" xxxxxxxxxxxxxxxxxxx"+query.toString());
		System.out.println("parent id xxxxxxxxxxxxxxxxxxx"+contacts.size());
		return  contacts;
	}
	
	@SuppressWarnings("unchecked")
	public Contact getContactByParent( String typeId,Integer empId) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact where isDeleted= :isDeleted and creator.id=:empId and viewType=:typeId");
		query.setParameter("isDeleted", false);
		query.setParameter("empId", empId);
		query.setParameter("typeId", typeId);
		List<Contact> contacts= new ArrayList<Contact>();
		contacts = query.list();
		System.out.println(" xxxxxxxxxxxxxxxxxxx"+query.toString());
		System.out.println("parent id xxxxxxxxxxxxxxxxxxx"+contacts.size());
		if(contacts.size()>0){
			return  contacts.get(0);
		}
		else{
			return null;
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Contact> getAllContact(Integer index) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact where isDeleted= :isDeleted");
		query.setParameter("isDeleted", false);
		query.setFirstResult((index*5));
		query.setMaxResults(5);
		List<Contact> contacts= new ArrayList<Contact>();
		contacts = query.list();
		return  contacts;
	}
	
	@SuppressWarnings("unchecked")
	public List<Contact> getAllContactForCompany(Integer index,Integer companyName,Integer count) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact con where con.isDeleted= :isDeleted and con.creator.id = :companyName");
		query.setParameter("isDeleted", false);
		query.setParameter("companyName", companyName);
		query.setFirstResult((index*count));
		query.setMaxResults(count);
		List<Contact> contacts= new ArrayList<Contact>();
		contacts = query.list();
		return  contacts;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public Integer getAllContactForCompanySize(Integer companyName) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact con where isDeleted= :isDeleted and con.creator.id = :companyName");
		query.setParameter("isDeleted", false);
		query.setParameter("companyName", companyName);
		List<Contact> contacts= new ArrayList<Contact>();
		contacts = query.list();
		return  contacts.size();

	}
	
	
	@SuppressWarnings("unchecked")
	public Integer getAllContactSize() {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact where isDeleted= :isDeleted");
		query.setParameter("isDeleted", false);
		List<Contact> contacts= new ArrayList<Contact>();
		contacts = query.list();
		return  contacts.size();

	}

	@SuppressWarnings("unchecked")
	public List<Contact> getAllContactbyCompany(String companyName) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Contact con where con.isDeleted= :isDeleted and con.creator.company.companyName = :companyName");
		query.setParameter("isDeleted", false);
		query.setParameter("companyName", companyName);
		List<Contact> contacts= new ArrayList<Contact>();
		contacts = query.list();
		return  contacts;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Contact> getAllContactByName(String name,String companyName) {
		Session session =sessionFactory.getCurrentSession();
		
        Criteria criteria = session.createCriteria(Contact.class).createAlias("creator.company", "company");
        criteria.add(Restrictions.like("fName", "%" + name + "%"));
        criteria.add(Restrictions.eq("isDeleted", false));
        criteria.add(Restrictions.eq("company.companyName", companyName));
        List results = criteria.list();
        if(results.size()>0){
        	return  results;
        }
        else{
        	return null;
        }
		
	}
	
	public void deleteContact(Integer id) {
		Contact contact = (Contact) sessionFactory.getCurrentSession().load(
				Contact.class, id);
		if (null != contact) {
			sessionFactory.getCurrentSession().delete(contact);
		}
		
		
	}

	
	
	
}
